package com.inspur.gs.sbssdl.dzyhpgl.dzyhplygl.dzyhplygl.back.bizactions;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.bef.api.parameter.retrieve.RespectiveRetrieveResult;
import com.inspur.edp.bef.api.BefRtBeanUtil;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

public class sbssDzyhpYhpsllqqrMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	private String userId;
	public sbssDzyhpYhpsllqqrMgrAction(IBEManagerContext managerContext, String id, String userId) {
		super(managerContext);
		this.id = id;
		this.userId = userId;
	}
	@Override
	public  void execute() {
		String configID ="com.inspur.gs.sbssdl.dzyhpgl.dzyhplygl.dzyhplygl.back.sbssDzyhpYhpsl";
		IStandardLcp lcp = BefRtBeanUtil.getLcpFactory().createLcp(configID);
		RespectiveRetrieveResult retrieve = lcp.retrieve(id);
		IEntityData data = retrieve.getData();

		ModifyChangeDetail change = new ModifyChangeDetail(id);
		change.putItem("isChecked", true);
		change.putItem("checkTime", new Date());
		AssociationInfo info = new AssociationInfo();
		info.setValue("checkId",userId);
		change.putItem("checkId", info);
		addKcJl(data);

		lcp.modify(change);
		lcp.save();
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}


	private void addKcJl(IEntityData data){
		// 库存表ID
		String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

		IStandardLcp kcLcp = BefRtBeanUtil.getLcpFactory().createLcp(kcCfg);

		// 上账编号
		String szbh = data.getValue("szbh").toString();
		// 归口部门
		String gkbmId = ((IValuesContainer) data.getValue("gkbm")).getValue("gkbm").toString();

		IEntityDataCollection yhpmx = data.getChilds().get("yhpmx");

		yhpmx.forEach(mx->{
			IEntityData kcData = kcLcp.retrieveDefault();

			ModifyChangeDetail kcDetail = new ModifyChangeDetail(kcData.getID());
			BigDecimal sl = new BigDecimal(mx.getValue("sl").toString());
			BigDecimal dj = new BigDecimal(mx.getValue("dj").toString());

			// 数量 单价
			kcDetail.getPropertyChanges().put("tsl", mx.getValue("sl"));
			// sl - sysl = 冻结数量
			kcDetail.getPropertyChanges().put("sysl", mx.getValue("sl"));

			kcDetail.getPropertyChanges().put("zj",sl.multiply(dj));
			kcDetail.getPropertyChanges().put("fromType", "3");

			// 上账单号
			AssociationInfo lydCodeInfo = new AssociationInfo();
			lydCodeInfo.setValue("lydCode",data.getID());
			kcDetail.getPropertyChanges().put("lydCode", lydCodeInfo);

			// 使用人员
			AssociationInfo syryInfo = new AssociationInfo();
			syryInfo.setValue("syryid",((IValuesContainer) mx.getValue("syry")).getValue("syry").toString());
			kcDetail.getPropertyChanges().put("syryid", syryInfo);

			// 归口部门
			AssociationInfo gkbmInfo = new AssociationInfo();
			gkbmInfo.setValue("gkbmid",gkbmId);
			kcDetail.getPropertyChanges().put("gkbmid", gkbmInfo);

			// 物料信息
			kcDetail.getPropertyChanges().put("mxId", mx.getValue("mxId"));
			kcDetail.getPropertyChanges().put("wlId", mx.getValue("wlId"));
			kcDetail.getPropertyChanges().put("wlMc", mx.getValue("wlMc"));
			kcDetail.getPropertyChanges().put("wlBh", mx.getValue("wlBh"));
			kcDetail.getPropertyChanges().put("wlLb", mx.getValue("wlLb"));
			kcDetail.getPropertyChanges().put("wlXh", mx.getValue("wlXh"));
			kcDetail.getPropertyChanges().put("wlDw", mx.getValue("wlDw"));
			kcDetail.getPropertyChanges().put("wlDj", dj);

			ArrayList<String> nodeCodes = new ArrayList<>();
			nodeCodes.add("itemtras");
			ArrayList<String> hierarchyIds = new ArrayList<>();
			hierarchyIds.add(kcData.getID());
			IEntityData kcChild = kcLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

			ModifyChangeDetail mxDetail = new ModifyChangeDetail(kcChild.getID());
			mxDetail.getPropertyChanges().put("trasType","0");
			mxDetail.getPropertyChanges().put("trasStatus","1");
			mxDetail.getPropertyChanges().put("omission","0");
			mxDetail.getPropertyChanges().put("trasSl",mx.getValue("sl"));
			mxDetail.getPropertyChanges().put("djid",id);
			mxDetail.getPropertyChanges().put("djCode",szbh);

			kcDetail.addChildChangeSet("itemtras",mxDetail);

			kcLcp.modify(kcDetail);
		});

		kcLcp.save();
	}
}